package com.cortex.papaya.view;

import android.content.Context;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.GestureDetector;
import android.view.GestureDetector.OnGestureListener;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewTreeObserver.OnGlobalLayoutListener;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationUtils;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.Switch;

import com.cortex.papaya.utilities.Utilities;
import com.cortex.papaya.view.PapayaRowSearch.ISlidFinger;
import com.cortex.papayasearch.R;

public class PapayaHeaderView extends RelativeLayout{
	private PapayaRowSearch rowSearch;
	private ImageView imgLogo;
	private ImageView imgLines;
	private ImageView imgSearch;
	private OnActionHeaderListener onActionHeaderListener;
	private OnOrientationChangedListener onOrientationChangedListener;
	private LinearLayout llContainer;
	boolean isKeyboardVisible;


	public PapayaHeaderView(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	public PapayaHeaderView(Context context) {
		super(context);
	}
	@Override
	protected void onFinishInflate() {
		// TODO Auto-generated method stub
		super.onFinishInflate();
		init();

	}
	private void init(){
		imgLogo = (ImageView)findViewById(R.id.imgLogo);
		rowSearch = (PapayaRowSearch)findViewById(R.id.rowSearch);
		imgLines = (ImageView)findViewById(R.id.imgHeaderLines);
		llContainer = (LinearLayout)findViewById(R.id.llContainer);
		imgSearch = (ImageView)findViewById(R.id.imgSearch);
		initHandlers();
		showKeyboard();
	}

	private void initHandlers(){

		imgLines.setOnTouchListener(new OnTouchListener() {
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				RelativeLayout.LayoutParams params = (LayoutParams) getLayoutParams();
				int y = (int) event.getY();
				switch(event.getAction()){
				case MotionEvent.ACTION_DOWN:
					showAll();
					return true;
				case MotionEvent.ACTION_MOVE:
					
					params.topMargin += y;
					setLayoutParams(params);	
					break;
				case MotionEvent.ACTION_UP:
				case MotionEvent.ACTION_CANCEL:
					int vertical = LinearLayout.HORIZONTAL;
					boolean isOrientationChanged = false;
					if(getBottom() >= getResources().getDisplayMetrics().heightPixels-getHeight()){
						vertical = LinearLayout.VERTICAL;
						isOrientationChanged = true;
						setInSingleLine(!isOrientationChanged);
						if(llContainer.getOrientation() == vertical){
							if(onOrientationChangedListener != null){
								onOrientationChangedListener.orientationChanged(isOrientationChanged);
							}
						}
					}
					else if(getTop()<0){
						// hide header
						params.topMargin = -getHeight()+imgLines.getHeight();
					}
					else if(getTop() > 0 && getTop() < getResources().getDisplayMetrics().heightPixels-getHeight()){
						// set header in origin position
						params.topMargin = 0;
					}
					setLayoutParams(params);
					
					break;
				}
				return false;
			}
		});
		imgLogo.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if(onActionHeaderListener != null){
					onActionHeaderListener.onAction(ActionOnHeader.LOGO);
				}
				rowSearch.clear();
			}
		});
		// listener to slid right or slid left of row search
		rowSearch.setOnSlidFingerListener(new ISlidFinger() {

			@Override
			public void toRight() {
				if(onActionHeaderListener != null){
					onActionHeaderListener.onAction(ActionOnHeader.ROW_SEARCH);
				}
			}

			@Override
			public void toLeft() {
				// TODO Auto-generated method stub
			}
		});
		imgSearch.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				if(rowSearch.getValue().isEmpty()){
					rowSearch.setHint(getResources().getString(R.string.writeSomethingHere));
					return;
				}
				Utilities.showKeyboard(true, rowSearch);
				if(onActionHeaderListener != null){
					onActionHeaderListener.onAction(ActionOnHeader.ROW_SEARCH);
				}
			}
		});
	}
	public boolean isShow() {
		boolean retValue = false;
		if(imgLogo.getVisibility() == VISIBLE){
			retValue = true;
		}
		return retValue;
	}
	public void setOnActionHeaderListener(OnActionHeaderListener onActionHeaderListener) {
		this.onActionHeaderListener = onActionHeaderListener;
	}
	public void setOnOrientationChangedListener(OnOrientationChangedListener onOrientationChangedListener) {
		this.onOrientationChangedListener = onOrientationChangedListener;
	}
	public void showLines(){
		imgLines.setVisibility(VISIBLE);
	}
	public void hideLines(){
		imgLines.setVisibility(GONE);
	}
	public void showAll(){
		llContainer.setVisibility(VISIBLE);
	}
	public void hideAll(){
		llContainer.setVisibility(GONE);
	}
	public void setInSingleLine(boolean isSingleLine){
		RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)getLayoutParams();
		if(isSingleLine){
			llContainer.setOrientation(LinearLayout.HORIZONTAL);
			params.addRule(RelativeLayout.CENTER_IN_PARENT,0);
			params.addRule(RelativeLayout.ALIGN_PARENT_TOP);
			params.leftMargin = 0;
			params.rightMargin = 0;
			params.topMargin = 0;
		}
		else{
			llContainer.setOrientation(LinearLayout.VERTICAL);
			params.addRule(RelativeLayout.CENTER_IN_PARENT);
			params.addRule(RelativeLayout.ALIGN_PARENT_TOP,0);
			params.leftMargin = 50;
			params.rightMargin = 50;
			params.topMargin = 0;
		}
		setLayoutParams(params);
	}

	public String getRowSearchText(){
		return rowSearch.getValue();
	}
	private void showKeyboard() {
		getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
			@Override
			public void onGlobalLayout() {
				Rect r = new Rect();
				// r will be populated with the coordinates of your view
				// that area still visible.
				getWindowVisibleDisplayFrame(r);

				int heightDiff = getRootView().getHeight()
						- (r.bottom - r.top);
				if (heightDiff > 100) { // if more than 100 pixels, its
					isKeyboardVisible = true;
				} else
					isKeyboardVisible = false;
			}
		});

	}
	public interface OnActionHeaderListener{
		void onAction(ActionOnHeader action);
	}
	public interface OnOrientationChangedListener{
		void orientationChanged(boolean isVertical);
	}
	public enum ActionOnHeader{
		LOGO,
		ROW_SEARCH,
		LINES
	}
}
